#include "filepath.h"

#include "delegate.h"

#include "../sleipner_config.h"
#ifndef _PLATFORM_WII

#include <stdlib.h>
#include <ctype.h>

#if defined(__APPLE__) || defined(_PLATFORM_ANDROID)
#define _MAX_DRIVE 100
#define _MAX_DIR   3
#define _MAX_FNAME 100
#define _MAX_PATH  256
#define  _MAX_EXT  256

#include <stddef.h>
#define ISSLASH(c) ( c=='/' )
#define EXT_DELIMIT '.'

static char* GetBaseName (const char* name)
{
	char const *base = name;
	char const *p;
	for (p = base; *p; p++)
    {
		if ( ISSLASH (*p) )
        {
			// Treat multiple adjacent slashes like a single slash.  
			do p++;
			while (ISSLASH (*p));
			
			// If the file name ends in slash, use the trailing slash as
            // the basename if no non-slashes have been found.  
			if (! *p)
            {
				if (ISSLASH (*base))
					base = p - 1;
				break;
            }
			
			// *P is a non-slash preceded by a slash.  
			base = p;
        }
    }
	return (char *) base;
}

static char* SplitFileName(char *dest,char* ext, const char *name)
{
	char *p=dest;
    strcpy(dest, GetBaseName(name) );
    while(strchr(dest,EXT_DELIMIT)!=NULL)
    {
        if(strlen(p) < 2) 
            break;
        if(*p)
        {
            p+=strlen(p);
            while(*p!=EXT_DELIMIT) 
				--p;
			
			if(strlen(p) == 4)
				strcpy(ext ,p);
			else
				*ext = 0x0;
						
            if(!*p)
            {
				--p;
				break;
            }
            else    
				*p=0x0;
        }
    }
    return dest;
}   

#endif

bool FilePath::FileExists(const std::string& Path)
{
	/* std version */
	if (FILE* fp = fopen(Path.c_str(), "r")) {
		fclose(fp);
		return true;
	}

	return false;
}

bool FilePath::GetNextSequence(const std::string& Path, std::string& Output, bool bNext)
{
	char drive[_MAX_DRIVE];
	char dir[_MAX_DIR];
	char fname[_MAX_FNAME];
	char ext[_MAX_EXT];
	char outpath[_MAX_PATH];
	char szFormat[256];

	int len, startidx, val;

#if defined(__APPLE__) || defined(_PLATFORM_ANDROID)
	SplitFileName(fname,ext,Path.c_str());
#else
	_splitpath(Path.c_str(), drive, dir, fname, ext);
#endif

	len = strlen(fname);
	if (len < 1) return false; // invalid file name

	for (startidx = (len - 1); startidx >= 0; startidx--)
	{
		if (!isdigit(fname[startidx]))
			break;
	}

	if (++startidx >= len) return false; // no trailing digits found

	val = atoi(&fname[startidx]) + (bNext ? 1 : -1);

	if (val < 0) return false; // invalid file name sequence

	fname[startidx] = 0;
	sprintf(szFormat, "%s%%.%dd", fname, (len - startidx)); // set format to ensure same digit system
	sprintf(fname, szFormat, val);

#if defined(__APPLE__) || defined(_PLATFORM_ANDROID)
	//CHIT: TODO: implement for this platform  
	//assert(0);
#else
	_makepath(outpath, drive, dir, fname, ext);
#endif
	
	Output = outpath;
	return true;
}

std::string FilePath::GetFNameFromPath(const std::string& Path, bool bIncludeExt)
{
	char drive[_MAX_DRIVE];
	char dir[_MAX_DIR];
	char fname[_MAX_FNAME];
	char ext[_MAX_EXT];
	
#if defined(__APPLE__) || defined(_PLATFORM_ANDROID)
	SplitFileName(fname,ext,Path.c_str());
#else
	_splitpath(Path.c_str(), drive, dir, fname, ext);
#endif
	

	if (bIncludeExt)
		return (std::string(fname) + ext);
	else
		return fname;
}

std::string FilePath::GetExtFromPath(const std::string& Path)
{
	char drive[_MAX_DRIVE];
	char dir[_MAX_DIR];
	char fname[_MAX_FNAME];
	char ext[_MAX_EXT];

#if defined(__APPLE__) || defined(_PLATFORM_ANDROID)
	SplitFileName(fname,ext,Path.c_str());
#else
	_splitpath(Path.c_str(), drive, dir, fname, ext);
#endif
	
	return ext;
}

std::string FilePath::GetDirFromPath(const std::string& Path, bool bIncludeDrive)
{
	char drive[_MAX_DRIVE];
	char dir[_MAX_DIR];
	char fname[_MAX_FNAME];
	char ext[_MAX_EXT];

#if defined(__APPLE__) || defined(_PLATFORM_ANDROID)
	SplitFileName(fname,ext,Path.c_str());
	//CHIT: TODO: implement for this platform  
	//assert(0);

#else
	_splitpath(Path.c_str(), drive, dir, fname, ext);
#endif
	
	if (bIncludeDrive) {
		char outpath[_MAX_PATH];
		strncpy(outpath, Path.c_str(), strlen(drive) + strlen(dir));
		return outpath;
	}
	else
		return dir;
}

#endif // _PLATFORM_WII
